<template>
	<view class="destination">
		<my-header>
			<view class="header-box">
				<text class="header-box-item">{{text}}</text>
			</view>
		</my-header>
		<view class="destination-word">
			<view v-for="(item,index) in products" :key="item.id" class="destination-word-item">
				<view>
					<view>{{item.name}}</view>
					<view>{{item.price}}￥</view>
				</view>
				<view @click="pathPlanning(item.latitude,item.longitude,item.name)">
					<image src="../../static/navigation.png" />
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import MyHeader from '../../components/MyHeader/MyHeader.vue'
	import QQMapWX from '../../utils/qqmap-wx-jssdk.js';
	var qqmapsdk;
	import {
		space
	} from '../../utils/distance.js'
	export default {
		components: {
			MyHeader
		},
		data() {
			return {
				text: '北京市',
				latitude: '0', // 纬度
				longitude: '0', // 经度
				productList: [{
					id: '1',
					name: '杨柳青庄园',
					latitude: '39.16146', // 纬度
					longitude: '116.98010', // 经度
					price: '20'
				}, {
					id: '2',
					name: '天津之眼',
					latitude: '39.15970', // 纬度
					longitude: '117.19305', // 经度
					price: '120'
				}]
			}
		},
		computed: {
			products() {
				let list = this.productList;
				list = list.map(item => {
					item.distance = space(this.latitude, this.longitude, item.latitude, item.longitude)
					return item
				}).sort(function(a, b) {
					return a.distance - b.distance
				})
				return list || [];
			}
		},
		created() {
			this.getAddress()
		},
		methods: {
			pathPlanning(lat, lng, name) {
				uni.navigateTo({
					url: `/pages/pathPlanning/pathPlanning?latitude=${this.latitude}&longitude=${this.longitude}&lat=${lat}&lng=${lng}&title=${name}`
				})
			},
			getAddress() {
				qqmapsdk = new QQMapWX({
					key: '2UUBZ-ZFYWG-TD6QX-QLD5O-XYQ2V-CIFUC'
				})
				qqmapsdk.reverseGeocoder({
					success: (res) => {
						console.log(res)
						this.text = res.result.ad_info.city;
						this.latitude = res.result.location.lat; // 纬度
						this.longitude = res.result.location.lng;
					},
					fail: (err) => {
						console.log(err)
						uni.showToast({
							title: "拒绝授权位置信息"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f4f4f4;
	}

	.destination {

		// padding: 20upx;
		.header-box {
			&-item {
				margin-right: 10upx;

				&.active {
					font-size: 38upx;
					font-weight: bold;
				}
			}
		}

		&-view {
			display: grid;
			grid-template-columns: 1fr 1fr;
			grid-gap: 10px 10px;
			padding: 20upx;

			&-item {
				background: paleturquoise;
				height: 0;
				padding-bottom: 100%;
				/*高度设置为0，使用内间距100%，将匡高设置为一致*/
				position: relative;

				image {
					width: 100%;
					height: 100%;
					position: absolute;
					object-fit: cover;
					/*预览图片的缩略图最好基于正中心显示*/
				}

				.room {
					font-size: 24upx;
					color: #fff;
					font-weight: bold;
					position: absolute;
					z-index: 100;
					bottom: 0;
					height: 62upx;
					line-height: 62upx;
					padding: 0 20upx;
					box-sizing: border-box;
					background: rgba(23, 174, 240, 1);
					border-top-right-radius: 50upx;
					border-bottom-right-radius: 50upx;
				}

			}
		}

		&-word-item {
			background: #fff;
			height: 80upx;
			padding: 20upx;
			margin-bottom: 20upx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			image {
				width: 30px;
				height: 30px;
			}
		}
	}
</style>